package com.cci.kangdao.controller;

import com.cci.kangdao.dao.ExternalAuthMapper;
import com.cci.kangdao.dao.model.ExternalAuthInfo;
import com.cci.kangdao.dto.BaseResponse;
import com.cci.kangdao.dto.PagedResponse;
import com.cci.kangdao.dto.request.ButlerRequest;
import com.cci.kangdao.dto.response.CustomerEngineRelResponse;
import com.cci.kangdao.model.ButlerInfo;
import com.cci.kangdao.model.CipHeader;
import com.cci.kangdao.service.CustomerEngineRelService;
import com.cci.kangdao.service.impl.SMSEnum;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.gexin.fastjson.JSON;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
public class CustomerEngineRelController {

	private Logger log = Logger.getLogger(this.getClass().getName());

	@Autowired
	private CustomerEngineRelService customerEngineRelService;
	@Resource
	private ExternalAuthMapper externalAuthMapper;

	/**
	 * 用户设备列表接口
	 * 根据用户id获取设备列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCustomerEngineRel.aspx")
	public ModelAndView getCustomerEngineRel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getCustomerEngineRel(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	/**
	 * 管家服务列表
	 * 根据用户id获取管家服务列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getButlerServiceList.aspx", method = RequestMethod.POST)
	public BaseResponse getButlerServiceList(@RequestBody ButlerRequest request){
		try{
			log.info("入参:" + request);
			PagedResponse<ButlerInfo> list = customerEngineRelService.getButlerServiceList(request);
			return BaseResponse.success(list);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
		}
		return BaseResponse.error("未知错误");
	}

	/**
	 * 修改管家服务
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateButlerService.aspx", method = RequestMethod.POST)
	public BaseResponse updateButlerService(@RequestBody ButlerInfo request){
		try{
			log.info("入参:" + request);
			if(request.getType() == 0 && CollectionUtils.isEmpty(request.getIdList())){
				return BaseResponse.error(SMSEnum.type17.getIndex(), "缺少主键id集合");
			}
			if(StringUtils.isBlank(request.getBusinessNo())){
				return BaseResponse.error(SMSEnum.type17.getIndex(), "缺少服务站编码");
			}
			boolean success = customerEngineRelService.updateButlerService(request);
			if(success){
				return BaseResponse.success();
			} else {
				return BaseResponse.error("保存失败");
			}
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
		}
		return BaseResponse.error("未知错误");
	}

	/**
	 * 用户设备分页列表接口
	 * 根据用户id获取设备列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getPageCustomerEngineList.aspx")
	public ModelAndView getPageCustomerEngineList(HttpServletRequest request,
												  HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getPageCustomerEngineList(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * 根据esn获取设备信息，  如果esn  已经删除，则取第一条数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCustomerEngineByEsnOrFirst.aspx")
	public ModelAndView getCustomerEngineByEsnOrFirst(HttpServletRequest request,
												  HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getCustomerEngineByEsnOrFirst(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	/**
	 * 获取用户历史设备列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getHistoryCustomerEngineRel.aspx")
	public ModelAndView getHistoryCustomerEngineRel(HttpServletRequest request,
											 HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getHistoryCustomerEngineRel(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	/**
	 * 根据esn获取用户设备详细接口
	 * 根据用户id获取设备列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCustomerEngineByEsn.aspx")
	public ModelAndView getCustomerEngineByEsn(HttpServletRequest request,
											 HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getCustomerEngineByEsn(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	/**
	 * 根据CustomerEngine表主键id获取设备信息详情
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCustomerEngineById.aspx")
	public ModelAndView getCustomerEngineById(HttpServletRequest request,
											   HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getCustomerEngineById(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * 用户添加设备信息接口
	 * @param request
	 * @return
	 */
	@RequestMapping("/addCustomerEngineRel.aspx")
	public ModelAndView addCustomerEngineRel(HttpServletRequest request,
			HttpServletResponse response) {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.addCustomerEngineRel(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}
	/**
	 * 用户修改设备信息接口
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateCustomerEngineRel.aspx")
	public ModelAndView updateCustomerEngineRel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.updateCustomerEngineRel(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}
	/**
	 * 用户删除设备信息接口
	 */
	@RequestMapping("/deleteCustomerEngineRel.aspx")
	public ModelAndView deleteCustomerEngineRel(HttpServletRequest request,
			HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.deleteCustomerEngineRel(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}



	/**
	 * 根据用户id获取大运设备列表
	 */
	@RequestMapping("/getDYCustomerEngineRel.aspx")
	public ModelAndView getDYCustomerEngineRel(HttpServletRequest request,
											 HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getDYCustomerEngineRel(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}



	/**
	 * 用户添加设备信息接口   因一键登录需求改版了用户绑定设备逻辑  统一绑定设备界面，为兼容新老用户这里新开发添加设备接口
	 * @param paramStr
	 * @return
	 */
	@RequestMapping("/newAddCustomerEngineRel.aspx")
	public ModelAndView newAddCustomerEngineRel(@RequestBody String paramStr, HttpServletRequest request,
												HttpServletResponse response) {
		try{
			log.info("接口传参为"+paramStr);
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("paramStr", paramStr);
			return customerEngineRelService.newAddCustomerEngineRel(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * wa 自动绑车的接口
	 *
	 * @param mapParam
	 * @return
	 */
	@RequestMapping("/waNewAddCustomerEngineRel.aspx")
	public ModelAndView waNewAddCustomerEngineRel(@RequestBody Map<String, String> mapParam, HttpServletRequest request, HttpServletResponse response) {

		try {
			String esn = MapUtils.getString(mapParam, "ESN");
			log.info("接口传参为" + esn);
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("ESN", esn);
			return customerEngineRelService.waNewAddCustomerEngineRel(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 用户修改设备信息接口   因一键登录需求改版了用户绑定设备逻辑  统一绑定设备界面，为兼容新老用户这里新开发修改设备接口
	 *
	 * @param paramStr
	 * @return
	 */
	@RequestMapping("/newUpdateCustomerEngineRel.aspx")
	public ModelAndView newUpdateCustomerEngineRel(@RequestBody String paramStr, HttpServletRequest request,
												   HttpServletResponse response) throws Exception {
		try {
			log.info("接口传参为"+paramStr);
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("paramStr", paramStr);
			return customerEngineRelService.newUpdateCustomerEngineRel(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * 服务站编号验证芯管家车辆
	 */
	@RequestMapping("/xgjVerifyVehicle.aspx")
	public ModelAndView xgjVerifyVehicle(@RequestBody String paramStr, HttpServletRequest request,
												   HttpServletResponse response) throws Exception {
		try {
			log.info("接口传参为"+paramStr);
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("paramStr", paramStr);
			return customerEngineRelService.xgjVerifyVehicle(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * 批量绑车
	 */
	@RequestMapping(value = "/batchBindVehicles.aspx", method = RequestMethod.POST)
	public ModelAndView batchBindVehicles(@RequestBody String paramStr, HttpServletRequest request,
										 HttpServletResponse response) throws Exception {
		try {
			log.info("接口传参为"+paramStr);
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("paramStr", paramStr);
			return customerEngineRelService.batchBindVehicles(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * 批量绑车并签协议
	 */
	@RequestMapping(value = "/batchBindVehiclesSign.aspx", method = RequestMethod.POST)
	public ModelAndView batchBindVehiclesSign(@RequestBody String paramStr, HttpServletRequest request,
										  HttpServletResponse response,@RequestHeader Map<String, String> headerMap) throws Exception {
		try {
			log.info("接口传参为"+paramStr);
			CipHeader header = JSON.parseObject(JSON.toJSONString(headerMap), CipHeader.class);
			log.info( "header信息:" + header);
			if(null == header){
				ParameterTool.writeResponse(response, 17,"header为空！");
				return null;
			}
			if(StringUtils.isBlank(header.getSource()) || StringUtils.isBlank(header.getVersion()) || StringUtils.isBlank(header.getToken())){
				ParameterTool.writeResponse(response, 17,"header参数为空！");
				return null;
			}

			ExternalAuthInfo authInfo = externalAuthMapper.getAuthBySource(header.getSource(),header.getVersion());
			if(null == authInfo){
				ParameterTool.writeResponse(response, 17,"认证信息错误！");
				return null;
			}
			if(!authInfo.getToken().equals(header.getToken())){
				ParameterTool.writeResponse(response, 17,"token错误！");
				return null;
			}
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("paramStr", paramStr);
			return customerEngineRelService.batchBindVehiclesSign(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * 根据esn校验是否符合
	 * @param esn
	 * @return
	 */
	@RequestMapping(value = "esnByCheck.aspx")
	public ModelAndView esnByCheck(String esn,HttpServletResponse response) {
		boolean success = false;
		if (StringUtils.isNotBlank(esn)) {
			success = customerEngineRelService.esnByCheck(esn);
			ParameterTool.writeSuccessResponse(response, success);
		} else {
			ParameterTool.writeResponse(response, 17,"参数不能为空");
		}
		return null;
	}

	/**
	 * 根据esn获取最新设备详细
	 * @param esn
	 * @return
	 */
	@RequestMapping("/getEngineByEsn.aspx")
	public ModelAndView getEngineByEsn(String esn, HttpServletResponse response) {
		try{
			if (StringUtils.isNotBlank(esn)) {
				CustomerEngineRelResponse esnResponse = customerEngineRelService.getEngineByEsn(esn);
				ParameterTool.writeSuccessResponse(response, esnResponse);
			} else {
				ParameterTool.writeResponse(response, 17,"参数不能为空");
			}
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}



	/**
	 * 根据esn获取所有绑定该esn的用户信息（除了本人）
	 */
	@RequestMapping("/getCustomerEngineRelByEsn.aspx")
	public ModelAndView getCustomerEngineRelByEsn(HttpServletRequest request,
											   HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getCustomerEngineRelByEsn(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	/**
	 * 车主解除授权接口
	 */
	@RequestMapping("/carOwnerUnBindAuth.aspx")
	public ModelAndView carOwnerUnBindAuth(HttpServletRequest request,
										   HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.carOwnerUnBindAuth(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}



	/**
	 * 获取用户商用车列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/getCommercialCarList.aspx")
	public ModelAndView getCommercialCarList(HttpServletRequest request,
											 HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getCommercialCarList(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}


	/**
	 * 提交完善车辆信息接口（油耗导航）
	 * @param paramStr
	 * @return
	 */
	@RequestMapping("/submitImproveCarMsg.aspx")
	public ModelAndView submitImproveCarMsg(@RequestBody String paramStr, HttpServletRequest request,
												HttpServletResponse response) {
		try{
			log.info("submitImproveCarMsg接口传参为"+paramStr);
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			map.put("paramStr", paramStr);
			return customerEngineRelService.submitImproveCarMsg(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 用户添加设备信息和amt的接口
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/addCustomerEngineAndAmt.aspx")
	public ModelAndView addCustomerEngineAndAmt(HttpServletRequest request,
												HttpServletResponse response) {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.addCustomerEngineAndAmt(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	@RequestMapping("/getCustomerEngineRelAndAmt.aspx")
	public ModelAndView getCustomerEngineRelAndAmt(HttpServletRequest request,
												   HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.getCustomerEngineRelAndAmt(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	@RequestMapping("/updateCustomerEngineRelAmt.aspx")
	public ModelAndView newUpdateCustomerEngineRelAmt(HttpServletRequest request,
													  HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.updateCustomerEngineRelAmt(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}



	@RequestMapping("/deleteCustomerEngineRelAmt.aspx")
	public ModelAndView deleteCustomerEngineRelAmt(HttpServletRequest request,
												   HttpServletResponse response) {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return customerEngineRelService.deleteCustomerEngineRelAmt(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	@RequestMapping("/updateAssociationType.aspx")
	public void updateAssociationType(HttpServletRequest request,
									  HttpServletResponse response) {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			customerEngineRelService.updateAssociationType(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			ParameterTool.writeErrorResponse(response);
		}
	}



}
